<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	
<%@include file="/include/head.jsp"%>

<div id="cc" class="easyui-layout" style="width:100%;height:100%;">   
	<div data-options="region:'center',title:'销售订单-<fmt:formatDate value="${vo.orderDate}" pattern="yyyy-MM-dd"/>信息'" style="padding:5px;background:#eee;">
		<form id="editForm"   method="post" enctype="multipart/form-data" >
			<input name="id" type="hidden" value="${vo.id}">
			<input name="cusId" id="cusId" type="hidden" value="${vo.cusId}">
				<!-- 提交的时候存放子表订单明细信息 -->
				<input id="orderbData" name="orderbData" type="hidden" value="">
			<table cellpadding="0" cellspacing="1" class="formtable">
				<tr>
				<td align="right"><label class="Validform_label">客户:</label></td>
				<td class="value">
				<input class="easyui-textbox" type="text"  id="cusName" name="cusName"
				 data-options="required:true" value="${vo.cusName}" />
				</td>
				<td align="right"><label class="Validform_label">订单日期:</label></td>
				<td class="value">
				<input class="easyui-datebox" type="text"  id="orderDate" name="orderDate"
				 data-options="required:true" value="<fmt:formatDate value="${vo.orderDate}" pattern="yyyy-MM-dd"/>" />
				</td>
				</tr>
				<tr>
				<td align="right"><label class="Validform_label">订单类型:</label></td>
				<td class="value">
				<select class="easyui-combobox"    id="orderType" name="orderType"
				 data-options="required:true"   >
				 <option value="合同订单" <c:if test="${vo.orderType=='合同订单'}"> selected="selected"</c:if> > 合同订单</option>
				 <option value="其它订单" <c:if test="${vo.orderType=='其它订单'}"> selected="selected"</c:if> >其它订单</option>
				 <option value="其它订单" <c:if test="${vo.orderType=='破损补货'}"> selected="selected"</c:if> >破损补货</option>
				 </select>
				</td>
				<td align="right"><label class="Validform_label">总额:</label></td>
				<td class="value">
				<input class="easyui-numberbox" type="text"  id="totalAmount" name="totalAmount"
				 data-options="required:true,min:0,precision:2,readonly:true" value="${vo.totalAmount}" />
				</td>
				</tr>
				<tr>
				<td align="right"><label class="Validform_label">状态:</label></td>
				<td class="value">
				<select id="status" class="easyui-combobox" name="status" style="width:200px;" data-options="required:true,readonly:true">  
					<option value="0" <c:if test="${vo.status==0}">selected="selected"</c:if>>草稿</option>   
					<option value="1" <c:if test="${vo.status==1}">selected="selected"</c:if>>已确认</option>    
					<option value="5" <c:if test="${vo.status==5}">selected="selected"</c:if>>送货中</option>  
					<option value="10" <c:if test="${vo.status==10}">selected="selected"</c:if>>已完成</option>    
				</select>
				</td>
				<td align="right"><label class="Validform_label">备注:</label></td>
				<td class="value">
				<textarea class="easyui-textbox" type="text" multiline="true" id="remark" name="remark"
				 data-options="width:300,height:60"   >${vo.remark}</textarea>
				</td>
				</tr>
				
			</table>
		</form>
		<div id="tt" class="easyui-tabs" style="width:100%;height:300px;"> 
		 	<div title="订单明细" data-options="closable:false" style="overflow:auto;">   
	        	<table id="dg_orderb"> </table>    
	     	</div>   
		</div>
	</div>
	<div data-options="region:'south'" style="height:35px;text-align: center;padding-top: 5px">
	 	<a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="save()" >保存</a>  
	 	<a id="btncancel" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="closeDialog()">关闭</a>  
	 </div>
</div>

<div id="importDialog" class="easyui-dialog" title="查询" style="width: 400px; height: 260px;"  
                 data-options="iconCls:'pag-list',modal:true,collapsible:false,minimizable:false,maximizable:false,resizable:false,closed:true,buttons:'#btnimport'">
                 
                 <form action="" id="importForm" method="post"  enctype="multipart/form-data">
	            <table cellpadding="0" cellspacing="1" class="formtable">
								<tr>
				<td align="right"><label class="Validform_label">文件:</label></td>
				<td class="value">
					<input class="easyui-filebox" type="text"  id="file" name="file" data-options="required:true"/>
				</td>
				</tr>
				<tr>
				<td align="right"><label class="Validform_label">是否区县统一订单:</label></td>
				<td class="value">
					<input type="radio" name="isKunshan" value="1">是 &nbsp;&nbsp;&nbsp;
					<input type="radio" name="isKunshan" value="0" checked="checked"> 否
				</td>
				</tr>
				<tr>
				<td align="right"><label class="Validform_label">区县:</label></td>
				<td class="value">
				<select id="area" class="easyui-combobox" name="area" >  
					<option value="张家港市" >张家港市</option>   
					<option value="昆山市">昆山市</option>    
				</select>
				</td>
				</tr>
				</table>  
<div style="text-align:right;margin-right:30px;"  id="btnimport">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="doimport()">确定</a>  
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="cancelimport()">取消</a>  
 </div>  
</div>
<script type="text/javascript">
var dialog = frameElement.dialog;
$(function() {
	$('#cusName').textbox({    
	    buttonText:'Search',    
	    onClickButton:function(){
	    	CustomerDialog('cusId','cusName',false,function(data){
	    		var selRow = data.data[0];
	    	})	;
	    }
	});
	$('#editForm').form({    
	    url:'saveOrUpdate.do', 
	    iframe:false,   
	    onSubmit: function(){ 
	    	//do your things on submint
	    },    
	    success:function(data){    
	    	data = JSON.parse(data);
	    	if(data.result == '1')
	    	{
	    		$.messager.alert("保存结果","保存成功",'info',function(){
	    			if($.isFunction(dialog.dialog('options').callback))
	    			{
	    				dialog.dialog('options').callback(data);
	    			}
	    			closeDialog();
		    	});	
	    	}
	    	else
	    	{
	    		$.messager.alert("保存结果","保存失败：" + data.message,'error',function(){
	    			
		    	});
	    	}
	    }    
	});
	
	    initEditableCurdDatagrid4Sub('orderb','dg_orderb',{
			title:null,
			pagination:false,
			model:'${vo.id}'==''?'add':'edit',
			height:300,
			nowrap:true,
			queryParams:{'orderhId':'${vo.id}'},
			baseUrl:'${ctx}/platform/orderb/',
			url:'${ctx}/platform/orderb/listAllData.do',
			saveUrl:'${ctx}/platform/orderb/saveOrUpdate.do',
			updateUrl:'${ctx}/platform/orderb/saveOrUpdate.do',
			destroyUrl:'${ctx}/platform/orderb/delById.do',
			onAdd:function(idx,row)
			{
				row.orderhId='${vo.id}';
			},
		    loadFilter: function(data){
				return data;
			},
			columns:[[
				        	{field:'extOrderNo',title:'外部订单编号',width:80,editor:{type:'textbox',options:{required:true}}},    
				        	{field:'productId',title:'产品ID',width:100,hidden:true,editor:{type:'textbox',options:{required:true}}},    
				        	{field:'productName',title:'产品名称',width:100,editor:{
				        			type:'textbox',
				        			options:{
				        					required:true,
				        					buttonText:'Search',    
				        				    onClickButton:function(){
				        				    	var curTextBox = $(this);
			        				    		var rowIndex = $(this).parents('.datagrid-row').attr('datagrid-row-index'); 
				        				    	ProductDialog('','',false,function(data){
				        				    		$($('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'productId'}).target).textbox('setValue', data.data.id);
				        				    		curTextBox.textbox('setValue',data.data.name);
				        				    		var ed = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'productSpec'});
				        				    		$(ed.target).textbox('setValue', data.data.spec);
				        				    		
				        				    		var ed2 = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'unit'});
				        				    		$(ed2.target).textbox('setValue', data.data.unit);

				        				    		var ed3 = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'unitPrice'});
				        				    		$(ed3.target).numberbox('setValue', data.data.sprice);
				        				    		
				        				    	})
				        				    }
				        				}
				        	}},    
				        	{field:'productSpec',title:'产品规格',width:100,editor:{type:'textbox',options:{required:true,multiline:true}}},    
				        	{field:'unit',title:'单位',width:30,editor:{type:'textbox',options:{required:true}}},    
				        	{field:'productNum',title:'数量',width:40,editor:{type:'numberbox',options:{
				        		required:true,
				        		onChange:function(newValue,oldValue){
				        			var curTextBox = $(this);
        				    		var rowIndex = $(this).parents('.datagrid-row').attr('datagrid-row-index'); 
			        		    	var ed3 = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'unitPrice'});
        				    		var unitPrice = $(ed3.target).numberbox('getValue');
        				    		
			        		    	var ed = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'totalPrice'});
        				    		$(ed.target).numberbox('setValue', unitPrice*newValue);
        				    		
			        		    }
				        	}},formatter:function(value,row,idx){
				        		if(value)
					        		return parseFloat(value).toFixed(0);
			        			return value;
					        }},    
				        	{field:'unitPrice',title:'售价',width:40,editor:{type:'numberbox',options:{
				        			required:true,
				        			min:0,
				        			precision:2,
				        			onChange:function(newValue,oldValue){
				        				var curTextBox = $(this);
	        				    		var rowIndex = $(this).parents('.datagrid-row').attr('datagrid-row-index'); 
				        		    	var ed3 = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'productNum'});
	        				    		var productNumber = $(ed3.target).numberbox('getValue');
	        				    		
				        		    	var ed = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'totalPrice'});
	        				    		$(ed.target).numberbox('setValue', productNumber*newValue);
	        				    		
				        		    }
				        		}},formatter:function(value,row,idx){
				        			if(value)
						        		return parseFloat(value).toFixed(2);
				        			return value;
						        }},    
				        	{field:'totalPrice',title:'总价',width:50,editor:{type:'numberbox',options:{
				        		required:true,
				        		min:0,
				        		precision:2,
				        		onChange:function(newValue,oldValue)
				        		{
				        			var orderbData = $("#dg_orderb").edatagrid('getData').rows;
							    	var totalPrice = 0.0;
							    	for(var i =0; i < orderbData.length;i++)
							    	{
							    		if(!orderbData[i].isNewRecord)
							    		{
								    		totalPrice =parseFloat(totalPrice) + parseFloat(orderbData[i].totalPrice);
							    		}
							    	}
							    	$("#totalAmount").numberbox('setValue',totalPrice + parseFloat(newValue));
				        		}
				        		}},formatter:function(value,row,idx){
				        			if(value)
						        		return parseFloat(value).toFixed(2);
				        			return value;
						        }},    
				        	{field:'recvId',title:'收货地址ID',width:100,hidden:true,editor:{type:'textbox',options:{required:true}}},    
				        	{field:'recvName',title:'收货单位',width:100,editor:{type:'textbox',options:{
				        		required:true,
				        		buttonText:'Search',    
	        				    onClickButton:function(){
	        				    	var curTextBox = $(this);
        				    		var rowIndex = $(this).parents('.datagrid-row').attr('datagrid-row-index'); 
	        				    	RecvAddressDialog('','',false,function(data){
	        				    		$($('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'recvId'}).target).textbox('setValue', data.data.id);
	        				    		curTextBox.textbox('setValue',data.data.cusName);
	        				    		var ed3 = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'recvAdd'});
	        				    		$(ed3.target).textbox('setValue', data.data.cusAddress);
	        				    		$($('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'recvContactor'}).target).textbox('setValue', data.data.contractor);
	        				    		var ed = $('#dg_orderb').edatagrid('getEditor', {index:rowIndex,field:'recvTel'});
	        				    		$(ed.target).textbox('setValue', data.data.telphone + "/" + data.data.cellphone);
	        				    	})		        				    	
	        				    }
				        		}}},    
				        	{field:'recvAdd',title:'收货地址',width:100,editor:{type:'textbox',options:{
				        		required:true,
				        		multiline:true
				        		}}},
				        	{field:'recvContactor',title:'联系人',width:60,editor:{type:'textbox',options:{required:true}}},
				        	{field:'recvTel',title:'收货电话',width:100,editor:{type:'textbox',options:{required:true}}},    
				        	{field:'isPlanSend',title:'是否安排送货',width:100,editor:{type:'numberbox',options:{required:false}},hidden:true},    
				        	{field:'sendDate',title:'送货日期',width:100,editor:{type:'datebox',options:{required:false}},hidden:true},    
				        	{field:'sendWay',title:'送货方式',width:100,editor:{type:'textbox',options:{required:false}},hidden:true},    
				        	{field:'deliveryman',title:'送货人',width:100,editor:{type:'textbox',options:{required:false}},hidden:true},    
				        	{field:'isSigned',title:'是否签收',width:100,editor:{type:'numberbox',options:{required:false}},hidden:true},    
				        	{field:'signer',title:'签收人',width:100,editor:{type:'textbox',options:{required:false}},hidden:true},    
				        	{field:'signDate',title:'签收日期',width:100,editor:{type:'datebox',options:{required:false}},hidden:true}    
				    ]] 
				     
		},[{
			text: '保存', 
	        iconCls: 'icon-save', 
	        handler: function() { 
	        	$('#dg_orderb').edatagrid('saveRow');
	        } 
		},'-',{
			text: '导入', 
	        iconCls: 'icon-add', 
	        handler: function() { 
				importFile();
	        } 
		},'-',{
			text: '清空', 
	        iconCls: 'icon-remove', 
	        handler: function() { 
	        	var rows  = $('#dg_orderb').edatagrid('getRows');
	        	$.messager.confirm('确认清空', '确认清空所有(' + rows.length +'条)数据么？', function(r){
	        		if (r){
	        			//$('#dg_orderb').edatagrid('loadData',{total:0,rows:[]});
	        			var rows  = $('#dg_orderb').edatagrid('getRows');
	        			
	        			for(var i =0; i< rows.length;i++)
	        			{
								$('#dg_orderb').edatagrid('destroyRow',i)
	        				
	        			}
	        		}
	        	});
	        } 
		},'-']);
	  
	    $('#importForm').form({    
		    url:'parseExcel.do',   
		    iframe:false,
		    onSubmit: function(param){ 
		    	//do your things on submint
		    	param.orderhId = '${vo.id}';
		    	$.messager.progress(); 
		    },    
		    success:function(data){    
		    	data = JSON.parse(data);
		    	$.messager.progress('close');
		    	if(data.result == '1')
		    	{
		    		$.messager.alert("保存结果","导入成功",'info',function(){
		    			$('#dg_orderb').edatagrid('reload');
		    			cancelimport();
			    	});	
		    	}
		    	else
		    	{
		    		$.messager.alert("保存结果","导入失败：" + data.message,'error',function(){
		    			
			    	});
		    	}
		    }    
		}); 
});
function importFile(){
	
	$("#importDialog").dialog('open');
}


function doimport(){
	if(!$("#importForm").form('validate'))
	{
		$.messager.alert("验证结果","请选择文件",'error',function(){
    	});
		return;
	}
	 var val=$('input:radio[name="isKunshan"]:checked').val();
	if(val == 1)
	{
		
	}
	$("#importForm").submit();  
}
function cancelimport(){
	
	$("#importDialog").dialog('close');  
	
}

function save(){
	//验证表单
	if(!$("#editForm").form('validate'))
	{
		
		$.messager.alert("验证结果","表单验证失败",'error',function(){
    	});
		return;
	}
	
	//提交表单
	$("#editForm").submit();  
}
function closeDialog()
{
	dialog.dialog("close");
}

</script>
